	class Validator {
		constructor({data = {},keyValidator = {}} = {}) {
			const isArray = data instanceof Array;
			const isObj = data instanceof Object;
			//是object对象类型
			if (!isArray && isObj) Object.keys(data).forEach(key => this[key] = data[key]);
			else throw new Error(`new Validator初始化时参数类型不能为${typeof(data)}`);
			
			this.keyArr = Object.keys(data);
			
			//这里是想要进行验证的字段集合
			this.keyValidator = keyValidator;

			return this.validator();
		}

		//确认表单验证
		onConfirm() {
			//用来验证表单是否完成
			let isConfirm = true;
			Object.keys(this.keyValidator).some(key => {
				// console.log(`${key}`,this[key])
				if (!this[key]) {
					uni.showToast({
						icon: 'none',
						title: '请将信息填写完整'
					})
					isConfirm = false;
					return true;
				} else if (!this.keyValidator[key](this[key])) {
					isConfirm = false;
					return true;
				}
			})
			if(isConfirm) {
				let form = this.getObj();
				console.log(form)
				return form;
			}else throw new Error('验证不通过');
		}
		
		//清空
		onEmpty(){
			Object.keys(this).forEach(key => {
				if(this.keyArr.indexOf(key) != -1) this[key] = '';
			})
		}
		
		//返回表单的字段数据，去掉那些函数
		getObj(){
			let obj = {};
			Object.keys(this).forEach(key => {
				if(this.keyArr.indexOf(key) != -1) obj[key] = this[key];
			})
			return obj;
		}

		validator() {
			return new Proxy(this, {
				get: (target, key) => {
					return target[key];
				},
				set: (target, key, val) => {
					const va = this.keyValidator[key];
					//如果存在验证函数就对该字段进行赋值验证
					if(va) va(val);
					//统计表单的那些字段名
					if(this.keyArr.indexOf(key) == -1) this.keyArr.push(key);
					target[key] = val;
					return true;
				}
			})
		}
	}

	export default Validator;
